#include "TestProcess.h"
#include "Utils\Defines.h"
#include "GraphicsManager.h"
#include "InputManager.h"
#include "Cameras\FPSCamera.h"
#include "Cameras\TPSCamera.h"
#include "Object3D.h"
#include "Math\Matrix44.h"

CTestProcess::CTestProcess() : CProcess(), 
	m_Speed( 0.01f ),
	m_pFPSCamera( new CFPSCamera( Vect3f(15.0f,0.0f,0.0f), Vect3f(0.0f,0.0f,-0.0f), new CObject3D()) ),
	m_pTPSCamera( new CTPSCamera( Vect3f(15.0f,0.0f,0.0f), Vect3f(0.0f,0.0f,-0.0f), new CObject3D()) ),
	m_Amount( 0.0f ), m_Angle( 0.0f ),  m_AngleMoon( 0.0f ), m_PaintAll(false)
{
}

CTestProcess::~CTestProcess()
{
}

void CTestProcess::Update(float32 ElapsedTime)
{
	m_FPS = 1.0f/ElapsedTime;

	CInputManager* pInputManager = CInputManager::GetSingletonPtr();

	m_Amount +=  0.01f;
	m_Angle  += ElapsedTime* 0.05f;
	m_AngleMoon += ElapsedTime*0.05;
	
	Vect3f CameraDirection( m_pCamera->GetDirection() );
	if( pInputManager->IsUpDown( IDV_KEYBOARD, KEY_TAB ) )
	{
		CFPSCamera* pProcessCamera = dynamic_cast<CFPSCamera*>(m_pCamera);
		if( pProcessCamera )
		{
			m_pCamera = m_pTPSCamera;
		}
		else
		{
			m_pCamera = m_pFPSCamera;
		}
		return;
	}

	if( pInputManager->IsDown( IDV_KEYBOARD, KEY_W ) )
	{
		m_pCamera->Move( CFPSCamera::FORWARD, m_Speed );
	}
	if( pInputManager->IsDown( IDV_KEYBOARD, KEY_S ) )
	{
		m_pCamera->Move( CFPSCamera::BACKWARD, m_Speed );
	}
	if( pInputManager->IsDown( IDV_KEYBOARD, KEY_A ) )
	{
		m_pCamera->Move( CFPSCamera::LEFT, m_Speed );
	}
	if( pInputManager->IsDown( IDV_KEYBOARD, KEY_D ) )
	{
		m_pCamera->Move( CFPSCamera::RIGHT, m_Speed );
	}
	if( pInputManager->IsDown( IDV_KEYBOARD, KEY_Q ) )
	{
		m_pCamera->Move( CFPSCamera::UP, m_Speed );
	}
	if( pInputManager->IsDown( IDV_KEYBOARD, KEY_E ) )
	{
		m_pCamera->Move( CFPSCamera::DOWN, m_Speed );
	}

	Vect3i delta = pInputManager->GetMouseDelta();
	if( pInputManager->IsDown( IDV_MOUSE, MOUSE_BUTTON_LEFT ) )
	{
		m_pCamera->AddYawPitch(delta.x,delta.y);
	}

	CTPSCamera* pTPSCam = dynamic_cast<CTPSCamera*>(m_pCamera);
	if(pTPSCam)
		pTPSCam->AddZoom(delta.z * m_Speed);
}

void CTestProcess::Init()
{
	m_pCamera = m_pFPSCamera;
}

void CTestProcess::Render()
{
#if _DEBUG
	RenderDebugInfo();
#endif

	CGraphicsManager* pGraphicsManager = GraphicsInstance;

	//pGraphicsManager->DrawAxis(5);
	//pGraphicsManager->DrawGrid(100,colORANGE,50,50);

	Mat44f identity;
	identity.SetIdentity();

	pGraphicsManager->DrawSphere( 2, colYELLOW, 20 );
	if( m_PaintAll )
	{
		// Sun
		Mat44f transformationSun;
		transformationSun.SetIdentity();
		transformationSun.RotByAngleY(m_Amount);
		pGraphicsManager->SetTransform( transformationSun );
		pGraphicsManager->DrawSphere( 2, colYELLOW, 20 );
		pGraphicsManager->SetTransform( identity );

		// Earth
		Mat44f transformationEarth;
		transformationEarth.SetIdentity();
		transformationEarth.RotByAngleY(m_Amount);

		Mat44f transformationEarthTrsf;
		transformationEarthTrsf.SetIdentity();
		transformationEarthTrsf.Translate( Vect3f( mathUtils::Cos(m_Angle) * 10.0f, 0.0f, mathUtils::Sin(m_Angle)* 10.0f ) );

		pGraphicsManager->SetTransform(  transformationEarth*transformationEarthTrsf );
		pGraphicsManager->DrawSphere( 1, colWHITE, 20 );
		pGraphicsManager->SetTransform( identity );

		//Moon
		Mat44f transformationMoon;
		transformationMoon.SetIdentity();
		transformationMoon.RotByAngleY(m_Amount);

		Mat44f transformationMoonTrsf;
		transformationMoonTrsf.SetIdentity();
		transformationMoonTrsf.Translate( Vect3f( mathUtils::Cos(m_AngleMoon)*2.0f, 0.0f, mathUtils::Sin(m_AngleMoon) * 2.0f ) );

		pGraphicsManager->SetTransform( transformationMoonTrsf * transformationMoon * transformationEarthTrsf );
		pGraphicsManager->DrawSphere( 0.2f, colBLACK, 20 );
	}

	pGraphicsManager->SetTransform( identity );

	pGraphicsManager->DrawCamera(m_pTPSCamera);
	pGraphicsManager->DrawCamera(m_pFPSCamera);
}

void CTestProcess::RenderDebugInfo()
{
	CProcess::RenderDebugInfo();
}